package br.ufrj.dcc.tp.cliente.gui.event;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JPanel;

public class RecipientEvent implements Runnable {
	private List<String> recipients;
	private ArrayList<JCheckBox> checkboxes;
	private JPanel painel;
	private EventType e;
	
	public RecipientEvent(ArrayList<JCheckBox> checkboxes, EventType e) {
		this.checkboxes = checkboxes;
		this.e = e;
	}
	
	public RecipientEvent(List<String> recipients, ArrayList<JCheckBox> checkboxes, JPanel painel, EventType e) {
		this(checkboxes, e);
		this.recipients = recipients;
		this.painel = painel;
	}

	@Override
	public void run() {
		if(e == EventType.CLEAR) {
			if(checkboxes == null)
				return;
			
			for( JCheckBox ck : checkboxes ) {
				ck.setSelected(false);
			}
		} else if(e == EventType.RECIPIENT) {
			for( JCheckBox check : checkboxes ) {
				painel.remove(check);
			}
			
			checkboxes.clear();
			
			for(String user : recipients ) {
				JCheckBox check = new JCheckBox(user);
				checkboxes.add(check);
				painel.add(check);
			}
			painel.revalidate();
		}
	}
}
